package com.woniuxy.base.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.base.mapper.BasePurityMapper;
import com.woniuxy.base.model.entity.BasePurity;
import com.woniuxy.base.model.entity.BasePurityRecord;
import com.woniuxy.base.service.BasePurityRecordService;
import com.woniuxy.base.service.BasePurityService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class BasePurityServiceImpl implements BasePurityService {
    @Resource
    private BasePurityMapper basePurityMapper;

    @Resource
    private BasePurityRecordService recordService;

    @Override
    public Page<BasePurity> getAll(int current, int size) {
        return basePurityMapper.selectPage(new Page<>(current, size),null);
    }

    @Transactional
    @Override
    public Boolean update(BasePurity basePurity, String account) {
        BasePurity record = basePurityMapper.selectById(basePurity.getId());
        BasePurityRecord purityRecord = new BasePurityRecord();
        purityRecord.setPurity(record.getPurity())
                        .setState(record.getState()).setMaterialId(record.getMaterialId())
                        .setDescription(record.getDescription()).setUpdateTime(record.getAddTime())
                        .setPurityId(record.getId()).setUpdatePerson(account);
        basePurityMapper.update(basePurity, new UpdateWrapper<BasePurity>().eq("id",basePurity.getId()));
        recordService.add(purityRecord);
        return true;
    }

    @Override
    public Boolean add(BasePurity basePurity) {
        basePurityMapper.insert(basePurity);
        return true;
    }

    @Override
    public Page<BasePurity> conditionSearch(BasePurity basePurity, Integer current, Integer size) {
        return basePurityMapper.selectPage(new Page<>(current,size),new QueryWrapper<BasePurity>(basePurity));
    }

    @Override
    public Page<BasePurity> keywordSearch(String keyword, Integer current, Integer size) {
        QueryWrapper<BasePurity> wrapper = new QueryWrapper<>();
        wrapper.like("purity",keyword).or().like("description",keyword);
        return basePurityMapper.selectPage(new Page<>(current,size),wrapper);
    }

    @Override
    public List<BasePurityRecord> detail(Integer purityId) {
        return recordService.detail(purityId);
    }

}
